package com.example.mobile2030606148tdhhomework0325;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvMainTitle;
    private EditText edtTxtMainShow;
    private Button btnMainBack;
    private Button btnMainDiv;
    private Button btnMainClc;
    private Button btnMainMul;
    private Button btnMainNum7;
    private Button btnMainNum8;
    private Button btnMainNum9;
    private Button btnMainSub;
    private Button btnMainNum4;
    private Button btnMainNum5;
    private Button btnMainNum6;
    private Button btnMainPersent;
    private Button btnMainNum1;
    private Button btnMainNum2;
    private Button btnMainNum3;
    private Button btnMainAdd;
    private Button btnMainNum0;
    private Button btnMainPoint;
    private Button btnMainEqu;



//    EditText etX;
//    EditText etY;
    String msga;
    String msgb;
    int a,b;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        btnMainNum0.setOnClickListener(this);
        btnMainNum1.setOnClickListener(this);
        btnMainNum2.setOnClickListener(this);
        btnMainNum3.setOnClickListener(this);
        btnMainNum4.setOnClickListener(this);
        btnMainNum5.setOnClickListener(this);
        btnMainNum6.setOnClickListener(this);
        btnMainNum7.setOnClickListener(this);
        btnMainNum8.setOnClickListener(this);
        btnMainNum9.setOnClickListener(this);
        btnMainClc.setOnClickListener(this);
        btnMainBack.setOnClickListener(this);
        btnMainAdd.setOnClickListener(this);
        btnMainEqu.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        Button btn = (Button) v;
        int id = btn.getId();
        switch (id) {
            case R.id.btn_main_num0:
                edtTxtMainShow.append("0");
                break;
            case R.id.btn_main_num1:
                edtTxtMainShow.append("1");
                break;
            case R.id.btn_main_num2:
                edtTxtMainShow.append("2");
                break;
            case R.id.btn_main_num3:
                edtTxtMainShow.append("3");
                break;
            case R.id.btn_main_num4:
                edtTxtMainShow.append("4");
                break;
            case R.id.btn_main_num5:
                edtTxtMainShow.append("5");
                break;
            case R.id.btn_main_num6:
                edtTxtMainShow.append("6");
                break;
            case R.id.btn_main_num7:
                edtTxtMainShow.append("7");
                break;
            case R.id.btn_main_num8:
                edtTxtMainShow.append("8");
                break;
            case R.id.btn_main_num9:
                edtTxtMainShow.append("9");
                break;
            case R.id.btn_main_clc:
                edtTxtMainShow.setText(null);
                break;
            case R.id.btn_main_back:

                String msg = edtTxtMainShow.getText().toString();
                if (msg.length() > 1) {
                    edtTxtMainShow.setText(msg.substring(0, msg.length() - 1));
                }
                break;
            case R.id.btn_main_add:
                msga=edtTxtMainShow.getText().toString();
                msgb=edtTxtMainShow.getText().toString();
                a=Integer.valueOf(msga);
                b=Integer.valueOf(msgb);
                edtTxtMainShow.append("+");
//                msga=edtTxtMainShow.getText().toString();
//                msgb=edtTxtMainShow.getText().toString();
//                Integer a=Integer.valueOf(msga);
//                Integer b =Integer.valueOf(msgb);
//
//                edtTxtMainShow.append("+");

//                 msga = edtTxtMainShow.getText().toString();
//                 msgb = edtTxtMainShow.getText().toString();
//                 Integer a = Integer.valueOf(msga);
//                 Integer b = Integer.valueOf(msga);
//                 int result=a+b;
//                 edtTxtMainShow.append("+"+result);


                break;
            case R.id.btn_main_equ:
//               msga=edtTxtMainShow.getText().toString();
//               msgb=edtTxtMainShow.getText().toString();

//                int b = Integer.parseInt(b().toString());
//                int z = x+y;
//                Result.setText("=");
//                edtTxtMainShow.append("="+Result);
//                edtTxtMainShow.append("="+Result);
                int result=a+b;
                edtTxtMainShow.append("="+result);
                break;


            case R.id.btn_main_mul:

                edtTxtMainShow.append("*");

                break;
            case R.id.btn_main_persent:

                edtTxtMainShow.append("/");

                break;
            case R.id.btn_main_sub:
                edtTxtMainShow.append("-");
                break;
        }

    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        edtTxtMainShow = (EditText) findViewById(R.id.edtTxt_main_show);
        btnMainBack = (Button) findViewById(R.id.btn_main_back);
        btnMainDiv = (Button) findViewById(R.id.btn_main_div);
        btnMainClc = (Button) findViewById(R.id.btn_main_clc);
        btnMainMul = (Button) findViewById(R.id.btn_main_mul);
        btnMainNum7 = (Button) findViewById(R.id.btn_main_num7);
        btnMainNum8 = (Button) findViewById(R.id.btn_main_num8);
        btnMainNum9 = (Button) findViewById(R.id.btn_main_num9);
        btnMainSub = (Button) findViewById(R.id.btn_main_sub);
        btnMainNum4 = (Button) findViewById(R.id.btn_main_num4);
        btnMainNum5 = (Button) findViewById(R.id.btn_main_num5);
        btnMainNum6 = (Button) findViewById(R.id.btn_main_num6);
        btnMainPersent = (Button) findViewById(R.id.btn_main_persent);
        btnMainNum1 = (Button) findViewById(R.id.btn_main_num1);
        btnMainNum2 = (Button) findViewById(R.id.btn_main_num2);
        btnMainNum3 = (Button) findViewById(R.id.btn_main_num3);
        btnMainAdd = (Button) findViewById(R.id.btn_main_add);
        btnMainNum0 = (Button) findViewById(R.id.btn_main_num0);
        btnMainPoint = (Button) findViewById(R.id.btn_main_point);
        btnMainEqu = (Button) findViewById(R.id.btn_main_equ);
    }
}











